import glob
import os
import shutil
import time
import pandas as pd
from GetDataOnly import get_data_only
from login import login_ing
from page import get_page
from 判断是否有广告弹窗 import yes_or_no
from 筛选excel import excel


def get_data(port, user_path, down_path, date_start, date_end, date_lastmonth, usernames):
    # 清空下载文件夹
    for root, dirs, files in os.walk(down_path):
        # 删除文件
        for file in files:
            file_path = os.path.join(root, file)
            os.remove(file_path)
    page = get_page(port, user_path, down_path)
    for x in range(100):
        try:
            page.get("https://fxg.jinritemai.com/ffa/multiShop")
            page.wait(5)
            while page('.auxo-btn auxo-btn-primary', timeout=2):
                page.click(".auxo-btn auxo-btn-primary", timeout=2)
                page.wait(3)
            if not page.eles(".index_roleItem__1-Hwe", timeout=2) and not (page('.ecom-menu-badge feige_badge__2xELP',timeout=1) or page('接待',timeout=1)):
                login_ing(page, usernames)
            elif page('.ecom-menu-badge feige_badge__2xELP', timeout=1) or page('接待',timeout=1):
                get_data_only(page, down_path, date_start, date_end, date_lastmonth)
                page.quit()
                break
            if page.eles(".index_roleItem__1-Hwe", timeout=5):
                ele_list = page.eles(".index_roleItem__1-Hwe", timeout=5)
                # 获取账号下的店铺数，并遍历
                for i in range(len(ele_list)):
                    ele_list = page.eles(".index_roleItem__1-Hwe", timeout=5)
                    # 进入店铺并获取数据
                    for ele in ele_list:
                        shop_name = '抖音小店-' + ele.ele(".index_introName__fRtLx").text
                        # 如果存在该店铺要获取的数据文件则跳过该店铺
                        if os.path.exists(f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx"):
                            continue
                        print(shop_name)
                        ele.click(by_js=True)
                        # 点击弹窗
                        if page.s_eles("我已知悉"):
                            time.sleep(6)
                            page("我已知悉").click(by_js=True)
                        page('@data-id=553').click(by_js=True)
                        page.wait(8)
                        # 如果更多筛选被遮挡，则点击关闭弹窗
                        if page('更多筛选').states.is_covered:
                            yes_or_no(page)
                            page.wait(1)
                        page('更多筛选').click(by_js=True)
                        # 设置查询导出时间
                        page('.auxo-picker-input auxo-picker-input-active').click(by_js=True)
                        if page(f'@title={date_start}', timeout=2) and page(f'@title={date_end}', timeout=2):
                            page(f'@title={date_start}').click(by_js=True)
                            page(f'@title={date_end}').click(by_js=True)
                        else:
                            while True:
                                page('.auxo-picker-header-prev-btn').click(by_js=True)
                                if page(f'@title={date_start}', timeout=2) and page(f'@title={date_end}', timeout=2):
                                    page(f'@title={date_start}').click(by_js=True)
                                    page(f'@title={date_end}').click(by_js=True)
                                    break
                        page('确定').click(by_js=True)
                        # 查询
                        page('.auxo-btn auxo-btn-dashed auxo-btn-sm').click(by_js=True)
                        # 如果没有数据，则跳过该店铺
                        if page('暂无数据', timeout=2):
                            print('暂无数据')
                            df = pd.DataFrame()
                            df.to_excel(f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx")
                            page.get("https://fxg.jinritemai.com/ffa/multiShop")
                            break
                        else:
                            page('导出售后单').click(by_js=True)
                            print('等待售后单导出')
                            page.wait.download_begin(timeout=600)
                            page.wait.downloads_done(timeout=600)
                            print('售后单导出成功')
                        page.wait(3)
                        # 获取文件夹中所有的.xlsx文件
                        xlsx_files = glob.glob(os.path.join(down_path, "*.xlsx"))
                        os.makedirs(f'C:\\抖音退款数据\\{date_lastmonth}', exist_ok=True)
                        shutil.move(xlsx_files[0], f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx")
                        # 修改EXCEL
                        excel(shop_name, date_lastmonth, f"C:\\抖音退款数据\\{date_lastmonth}\\{shop_name}.xlsx")
                        page.wait(1)
                        page.get("https://fxg.jinritemai.com/ffa/multiShop")
                        page.wait(5)
                        break
                page.quit()
                return '成功'
        except Exception as e:
            print(e)
